$.form = {};
$.form.submit = function(form){
    $.mcCube.tools.ajax.post(
         $(form).attr('action')
        , function(data){
            $(form).html(data);
        }
        , $(form).serialize()
    );

    return false;
};
$.form.textarea = function(value, config){
    if($.isPlainObject(value)){
        config = value;
        value  = null;
    } else if(!config){
        config = {};
    }

    var input = $('<textarea>'+ value +'</textarea>');
    if(value)        input.val(value);
    if(config.attrs) input.attr(config.attrs);

    return input;
};
$.form.text = function(value, config){
    return this.input('text', value, config);
};
$.form.numeric = function(value, config){
    return this.input('numeric', value, config);
};
$.form.date = function(value, config){
    return this.input('text', value, config);
};
$.form.hidden = function(value, config){
    return this.input('hidden', value, config);
};
$.form.button = function(value, config){
    return this.input('button', value, config);
};
$.form.input = function(type, value, config){
    if($.isPlainObject(value)){
        config = value;
        value  = null;
    } else if(!config){
        config = {};
    }

    var input = $('<input type="'+ type +'" />');
    if(value)        input.val(value);
    if(config.attrs) input.attr(config.attrs);

    return input;
};
$.form.select = function(options, values, config){
    if($.isPlainObject(values)){
        config = values;
        values = [];
    }
    else if(!$.isArray(values)){
        values = [values];
    }

    if(!config){
        config = {};
    }

    var select = '';
    if(config.voidOption)
        select += '<option>-</option>';

    for(var opKey in options){
        var value = opKey;
        var html  = options[opKey];
        if(config.value){
            value = html[config.value];
            html  = html[config.html];
        } else if(config.html){
            html  = html[config.html];
        }

        var selected = '';
        if($.inArray(opKey, values) > -1)
            selected = ' selected';

        select += '<option value="'+ value +'"'+ selected +'>'
            + html
        +'</option>';
    }

    select = $('<select>'+ select +'</select>');
    if(config.attrs)
        select.attr(config.attrs);

    return select;
};

